package ui.activity.card;

import static ui.MessageProtocol.REFRESH;

import java.util.List;
import java.util.Random;

import ui.activity.exchange.CaptureActivity;
import ui.activity.exchange.LocateActivity;
import ui.activity.user.LoginActivity;
import ui.model.card.CardListModel;
import ui.widget.card.CardListAdapter;
import ui.widget.card.DragListView;
import ui.widget.card.PullToRefreshListView.OnRefreshListener;
import ui.widget.card.SearchKeyListener;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import business.entity.Card;
import business.service.card.ICardListViewService;
import business.service.card.impl.CardListViewService;

import com.lab107team.R;

public class CardListActivity extends Activity implements OnClickListener, OnKeyListener, OnTouchListener, OnItemClickListener, Handler.Callback, OnRefreshListener, AnimationListener {

	private static final String TAG = CardListActivity.class.getSimpleName();

	private static final int[] WELCOME_BG = { R.drawable.welcome_1,	R.drawable.welcome_2 };

	private CardListModel model;
	private ICardListViewService service;

	private CardListAdapter mCardListAdapter;
	private LayoutInflater mLayoutInflater;
	private PopupWindow mPopAddMenu;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.card_list_activity_layout);
		model = new CardListModel();
		service = new CardListViewService((CardListModel) model, this);
		initializeWidget();
	}

	private void initializeWidget() {
		mLayoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		mCardListAdapter = new CardListAdapter(this);

		View view = mLayoutInflater.inflate(R.layout.card_list_popup_window, null);
		mPopAddMenu = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true);
		mPopAddMenu.setAnimationStyle(R.style.AnimationPreview);

		view.setOnKeyListener(this);
		view.setOnTouchListener(this);
		((ImageButton) findViewById(R.id.btn_card_list_refresh)).setOnClickListener(this);
		((ImageButton) findViewById(R.id.btn_card_list_add)).setOnClickListener(this);
		((EditText) findViewById(R.id.et_card_list_search)).addTextChangedListener(new SearchKeyListener(this));
		((ImageButton) view.findViewById(R.id.btn_card_list_hand_input)).setOnClickListener(this);
		((ImageButton) view.findViewById(R.id.btn_card_list_exchange)).setOnClickListener(this);
		((ImageButton) view.findViewById(R.id.btn_card_list_location)).setOnClickListener(this);
		((DragListView) findViewById(R.id.lv_card_list_cards)).setOnItemClickListener(this);
//		((PullToRefreshListView) findViewById(R.id.lv_card_list_cards))
//				.setOnRefreshListener(this);

		Random random = new Random();
		int index = random.nextInt(WELCOME_BG.length);
		((ImageView) findViewById(R.id.iv_card_list_welcome)).setBackgroundResource(WELCOME_BG[index]);
		AlphaAnimation alpha = new AlphaAnimation(1.0f, 0.0f);
		alpha.setStartTime(AnimationUtils.currentAnimationTimeMillis() + 1500);
		alpha.setDuration(2000);
		alpha.setAnimationListener(this);
		((ImageView) findViewById(R.id.iv_card_list_welcome)).setAnimation(alpha);
	}

	@Override
	protected void onStart() {
		updata();
		super.onStart();
	}

	@Override
	protected void onResume() {
		((DragListView) findViewById(R.id.lv_card_list_cards)).setAdapter(mCardListAdapter);
		super.onResume();
	}

	private void updata() {
		if (service.listAllCard()) {
			Log.i(TAG, "更新数据成功");
			mCardListAdapter.notifyDataSetChanged();
		}
	}

	public CardListAdapter getCardListAdapter() {
		return mCardListAdapter;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_card_list_add:
			onAddBtnClick(v);
			break;

		case R.id.btn_card_list_hand_input:
			onHandInputBtnClick(v);
			break;

		case R.id.btn_card_list_exchange:
			onExchangeBtnClick(v);
			break;

		case R.id.btn_card_list_refresh:
			onRefreshBtnClick(v);
			break;

		case R.id.btn_card_list_location:
			onLocationBtnClick(v);
			break;

		default:
			break;
		}
	}

	private void onAddBtnClick(View v) {
		Log.i(TAG, "添加按钮被点击");
		mPopAddMenu.showAsDropDown(v, 0, 0);
	}

	private void onHandInputBtnClick(View v) {
		Log.i(TAG, "手动输入按钮被点击");
		Intent intent = new Intent(CardListActivity.this,
				EditCardActivity.class);
		intent.putExtra("isAdd", true);
		startActivity(intent);
		mPopAddMenu.dismiss();
	}

	private void onExchangeBtnClick(View v) {
		Log.i(TAG, "交换按钮被点击");
		Intent intent = new Intent(CardListActivity.this, CaptureActivity.class);
		startActivity(intent);
		mPopAddMenu.dismiss();
	}

	private void onLocationBtnClick(View v) {
		Log.i(TAG, "定位按钮被点击");
		Intent intent = new Intent(CardListActivity.this, LocateActivity.class);
		startActivity(intent);
		mPopAddMenu.dismiss();
	}

	private void onRefreshBtnClick(View v) {
		Log.i(TAG, "刷新按钮被点击");
		Intent intent = new Intent(CardListActivity.this, LoginActivity.class);
		startActivity(intent);
	}

	@Override
	public boolean onKey(View v, int keyCode, KeyEvent event) {
		// 处理选择添加方式框弹出时，点击返回键的事件
		if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK) {
			mPopAddMenu.dismiss();
		}
		return false;
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// 处理选择添加方式框弹出时，点击其他地方的事件
		mPopAddMenu.dismiss();
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,	long id) {
		List<Card> cardsList = model.getCardList();
		Card card = cardsList.get(position - 1);
		Log.i(TAG, "名片: " + card.getName() + "被点击");

		Intent intent = new Intent(CardListActivity.this, CardDetailActivity.class);
		intent.putExtra("id", card.getId());
		intent.putExtra("position", position - 1);
		startActivity(intent);
	}

	public CardListModel getModel() {
		return model;
	}

	public ICardListViewService getService() {
		return service;
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case REFRESH:
			refresh();
			break;

		default:
			break;
		}
		return false;
	}

	public void refresh() {
		updata();
	}

	@Override
	public void onRefresh() {
		Intent intent = new Intent(CardListActivity.this, EditCardActivity.class);
		intent.putExtra("isAdd", true);
		startActivityForResult(intent, 0);
	}

//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		((PullToRefreshListView) findViewById(R.id.lv_card_list_cards))
//				.onRefreshComplete();
//		((PullToRefreshListView) findViewById(R.id.lv_card_list_cards))
//				.setSelection(1);
//		super.onActivityResult(requestCode, resultCode, data);
//	}

	@Override
	public void onAnimationStart(Animation animation) {
	}

	@Override
	public void onAnimationEnd(Animation animation) {
		((ImageView) findViewById(R.id.iv_card_list_welcome)).setVisibility(View.GONE);
	}

	@Override
	public void onAnimationRepeat(Animation animation) {
		
	}
}
